﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
using Google.GData.Client;
using WapCrawlerCore;
using WapCrawlerCore.WapCrawler;
using WapCrawlerLib;

namespace WapCrawler.PostCrawler
{
    public partial class frmViewPostCrawlers : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void FilterPostCrawler()
        {
            Session["wapCrawler"] = int.Parse(WapCrawlerDropDownList.SelectedValue);
            Session["topicCrawler"] = int.Parse(TopicCrawlerDropDownList.SelectedValue);
            GridView postCrawlerGridView = (GridView)UcPostCrawlerGridView.FindControl("PostCrawlerGridView");
            postCrawlerGridView.DataBind();
        }

        
        protected void btnUpdatePost_Click(object sender, EventArgs e)
        {
            GridView postCrawlerGridView = (GridView)UcPostCrawlerGridView.FindControl("PostCrawlerGridView");
            int wapCrawlerId = int.Parse(WapCrawlerDropDownList.SelectedValue);
            
            Parallel.ForEach<GridViewRow>(postCrawlerGridView.Rows.OfType<GridViewRow>(), row =>
            {
                CheckBox chkBx = (CheckBox)row.FindControl("chkSelect");
                if (chkBx != null && chkBx.Checked)
                {
                    int postCrawlerId = (int) postCrawlerGridView.DataKeys[row.RowIndex]["postCrawlerId"];
                    WapDataClassesDataContext dc = new WapDataClassesDataContext();
                    WapCrawlerLib.PostCrawler objPostCrawler = dc.PostCrawlers.Single(p => p.postCrawlerId == postCrawlerId);
                    WapSiteBase wap = GeneralWap.InitWapSite(wapCrawlerId);
                    //string postUrl = row.Cells[3].Text;
                    string postUrl = objPostCrawler.postUrl;
                    string topicName = TopicCrawlerDropDownList.SelectedItem.Text;

                    wap.GetItem(postUrl, topicName);
                }
            });
        }

        protected void btnAddMyPosts_Click(object sender, EventArgs e)
        {
            GridView postCrawlerGridView = (GridView)UcPostCrawlerGridView.FindControl("PostCrawlerGridView");
            int wapCrawlerId = int.Parse(WapCrawlerDropDownList.SelectedValue);
            WapSiteBase wap = GeneralWap.InitWapSite(wapCrawlerId);
            foreach (GridViewRow row in postCrawlerGridView.Rows)
            {
                CheckBox chkBx = (CheckBox)row.FindControl("chkSelect");
                if (chkBx != null && chkBx.Checked)
                {
                    wap.AddMyPost((int)postCrawlerGridView.DataKeys[row.RowIndex].Value);
                }
            }
        }

        protected void WapCrawlerDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void TopicCrawlerDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilterPostCrawler();
        }

        protected void WapCrawlerDropDownList_DataBound(object sender, EventArgs e)
        {
            //Session["wapCrawler"] = int.Parse(WapCrawlerDropDownList.SelectedValue);
            //FilterPostCrawler();
        }

        protected void TopicCrawlerDropDownList_DataBound(object sender, EventArgs e)
        {
            FilterPostCrawler();
        }

        protected void dropDownListNumberOfRows_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView postCrawlerGridView = (GridView)UcPostCrawlerGridView.FindControl("PostCrawlerGridView");
            if (dropDownListNumberOfRows.SelectedIndex != (dropDownListNumberOfRows.Items.Count - 1))
            {
                postCrawlerGridView.AllowPaging = true;
                postCrawlerGridView.PageSize = int.Parse(dropDownListNumberOfRows.SelectedItem.Value);
            }
            else
            {
                postCrawlerGridView.AllowPaging = false;
            }
        }

        
    }
}